'use strict';

const Controller = require('egg').Controller;

class StatusController extends Controller {

    constructor(ctx) {
        super(ctx);
        this.newStatus = {
          userid: 'string',
          status: 'string',
        };
    } 

    async index() {
        const { ctx } = this;
        const { group } = ctx.query;
        if (group) {
            const status = await ctx.service.status.getStatusByGroup(group);  
            if (status) {
                ctx.status = 200;
                ctx.body = status;
            } else {
                ctx.status = 404;
            }
        } else {
            const status = await ctx.service.status.getStatusList();  
            if (status) {
                ctx.status = 200;
                ctx.body = status;
            } else {
                ctx.status = 404;
            }
        }
    }

    async show() {
        const { ctx } = this;
        const status = await ctx.service.status.getStatus(ctx.params.id);  
        if (status) {
            ctx.status = 200;
            ctx.body = status;
        } else {
            ctx.status = 404;
        }
    }

    async new() {
        const { ctx } = this;
        // ctx.validate(this.newStatus, ctx.query);
        const { userid, username, group, status } = ctx.query;
        const res = await ctx.service.status.create(userid, username, group, status);
        if (res) {
            ctx.status = 201;
        } else {
            ctx.status = 409;
        }
        
    }
}

module.exports = StatusController;